package com.qmx.qmxapp.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.MainActivity;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.AuthModel;
import com.qmx.qmxapp.bean.LoginModel;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.callback.DialogCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.token.TokenManager;
import com.qmx.qmxapp.utils.SocketUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.next)
    Button next;
    @BindView(R.id.tv_re_forget)
    TextView tv_re_forget;
    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.edit_phone)
    EditText edit_phone;
    @BindView(R.id.edit_password)
    EditText edit_password;

    private Intent i = new Intent();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.next, R.id.tv_re_forget, R.id.tv_register})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.next:
                doLogin();
                break;
            case R.id.tv_re_forget:
                i.setClass(this, ForgotPasswordActivity.class);
                startActivity(i);
                break;
            case R.id.tv_register:
                i.setClass(this, RegisterAccountActivity.class);
                startActivity(i);
                break;
        }

    }


    private String getAccount() {
        String account = edit_phone.getText().toString();
        if (TextUtils.isEmpty(account)) {
            showToast("账号不能为空");
            return "";
        }
        return account;
    }

    private String getPassword() {
        String password = edit_password.getText().toString();
        if (TextUtils.isEmpty(password)) {
            showToast("密码不能为空");
            return "";
        }
        return password;
    }

    private void doLogin() {
        getAccount();
        getPassword();
        OkGo.<LzyResponse<LoginModel>>post(AppConstant.Url + ComParamContact.Login.PATH)
                .params(ComParamContact.Login.ACCOUNT, getAccount())
                .params(ComParamContact.Login.PASSWORD, getPassword())
                .execute(new DialogCallback<LzyResponse<LoginModel>>(this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<LoginModel>> response) {
                        AuthModel authModel = new AuthModel();
                        authModel.setToken(response.body().data.getUser_token().getToken());
                        TokenManager.getInstance().setAuthModel(authModel);

                        editor.putString(AppConstant.USER_ID, String.valueOf(response.body().data.getId()));
                        editor.putString(AppConstant.IDENTIFIER, response.body().data.getIdentifier());
                        editor.putString(AppConstant.USER_SIG, response.body().data.getUser_sig().getUsersig().toString());
                        editor.putString(AppConstant.MOBILE, response.body().data.getMobile());
                        editor.putInt(AppConstant.ROOM_ID, response.body().data.getRoom().getId());
                        editor.putString(AppConstant.ROOM_NAME, response.body().data.getRoom().getName());
                        editor.putString(AppConstant.AVATAR, response.body().data.getAvatar());
                        editor.commit();

                        //视频登录

                        //                        showToast(TokenManager.getInstance().getAuthModel().getToken());
                        SocketUtil.getInstance();
                        afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));

                        getHttpToken();


                        i.setClass(LoginActivity.this, MainActivity.class);
                        startActivity(i);
                        //                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<LzyResponse<LoginModel>> response) {
                        showToast("接口有问题");
                        Log.e("doLoginonError", response + "");
                        //                            handleError(response);
                    }
                });
    }
}
